<?php
class C_proyek extends CI_Controller {
	function __construct(){
		parent::__construct();
		checkLogin();
		$this->load->model('m_proyek');
	}
	function index(){
		$task = secure_post('task');
		switch($task){
			case 'LIST' :
				$this->_list();
				break;
			case 'INSERT' :
				$this->_insert();
				break;
			case 'UPDATE' :
				$this->_update();
				break;
			case 'DELETE' :
				$this->_delete();
				break;
			case 'DETAIL' :
				$this->_detail();
				break;
			default :
				$data["data_m_cabang_id"] = $this->m_proyek->getDataCabang();
				$data["data_m_proyek_jenis"] = array("","Reguler","JO");
				$data["data_proyek_sumberdana"] = array("","APBN", "APBD", "Swasta");
				$data["data_proyek_jeniskontrak"] = array("","Lump Sum", "Unit Price", "Gabungan", "Turn Key");
				$this->load->view('v_proyek',$data);
		}
	}
	
	function _list(){
		$data['page'] = numeric_post("page");
		$data['rows'] = numeric_post("rows");
		$data['proyek_tglserahterima1'] = nosql_post("proyek_tglserahterima1");
		$data['proyek_tglserahterima2'] = nosql_post("proyek_tglserahterima2");
		$data['proyek_kode'] = nosql_post("proyek_kode");
		$data['proyek_nama'] = nosql_post("proyek_nama");
		$data['m_cabang_id'] = nosql_post("m_cabang_id");
		$data['proyek_pm'] = nosql_post("proyek_pm");
		$data['proyek_jenis'] = nosql_post("proyek_jenis");
		$data['proyek_tahunmulai'] = nosql_post("proyek_tahunmulai");
		$data['proyek_nkawal'] = nosql_post("proyek_nkawal");
		$data['proyek_nkadd'] = nosql_post("proyek_nkadd");
		$data['proyek_nkakhir'] = nosql_post("proyek_nkakhir");
		$data['proyek_cp'] = nosql_post("proyek_cp");
		$data['proyek_kontruksijenis'] = nosql_post("proyek_kontruksijenis");
		$data['proyek_sumberdana'] = nosql_post("proyek_sumberdana");
		$data['proyek_jeniskontrak'] = nosql_post("proyek_jeniskontrak");
		$data['proyek_jaminanpelaksanaan'] = nosql_post("proyek_jaminanpelaksanaan");
		$data['proyek_jaminanpenawaran'] = nosql_post("proyek_jaminanpenawaran");
		$data['proyek_retensi'] = nosql_post("proyek_retensi");
		$data['proyek_denda'] = nosql_post("proyek_denda");
		$data['proyek_carapembayaran'] = nosql_post("proyek_carapembayaran");
		$data['proyek_uangmuka'] = nosql_post("proyek_uangmuka");
		$data['proyek_jangkawaktusetifikasi'] = nosql_post("proyek_jangkawaktusetifikasi");
		$data['proyek_jangkawaktupembayaran'] = nosql_post("proyek_jangkawaktupembayaran");
		echo $this->m_proyek->_list($data);
	}
	
	function _insert(){
		$data = array(
			'proyek_tglserahterima1' => ( date("Y-m-d",strtotime($this->input->post('proyek_tglserahterima1'))) == "1970-01-01")? null : date("Y-m-d",strtotime($this->input->post('proyek_tglserahterima1'))), 
			'proyek_tglserahterima2' => ( date("Y-m-d",strtotime($this->input->post('proyek_tglserahterima2')))== "1970-01-01")? null : date("Y-m-d",strtotime($this->input->post('proyek_tglserahterima2'))), 
			'proyek_kode' => $this->input->post('proyek_kode'), 
			'proyek_nama' => $this->input->post('proyek_nama'), 
			'm_cabang_id' => $this->input->post('m_cabang_id'), 
			'proyek_pm' => $this->input->post('proyek_pm'), 
			'proyek_jenis' => $this->input->post('proyek_jenis'), 
			'proyek_tahunmulai' => $this->input->post('proyek_tahunmulai'), 
			'proyek_nkawal' => $this->input->post('proyek_nkawal'), 
			'proyek_nkadd' => $this->input->post('proyek_nkadd'), 
			'proyek_nkakhir' => $this->input->post('proyek_nkakhir'), 
			'proyek_cp' => $this->input->post('proyek_cp'), 
			'proyek_kontruksijenis' => $this->input->post('proyek_kontruksijenis'), 
			'proyek_sumberdana' => $this->input->post('proyek_sumberdana'), 
			'proyek_jeniskontrak' => $this->input->post('proyek_jeniskontrak'), 
			'proyek_jaminanpelaksanaan' => $this->input->post('proyek_jaminanpelaksanaan'), 
			'proyek_jaminanpenawaran' => $this->input->post('proyek_jaminanpenawaran'), 
			'proyek_retensi' => $this->input->post('proyek_retensi'), 
			'proyek_denda' => $this->input->post('proyek_denda'), 
			'proyek_carapembayaran' => $this->input->post('proyek_carapembayaran'), 
			'proyek_uangmuka' => $this->input->post('proyek_uangmuka'), 
			'proyek_jangkawaktusetifikasi' => $this->input->post('proyek_jangkawaktusetifikasi'), 
			'proyek_jangkawaktupembayaran' => $this->input->post('proyek_jangkawaktupembayaran'), 
			  
			'proyek_sys_active'=> '1',
			'proyek_sys_owner'	=>$_SESSION[SESSION_USERID]);
		$this->m_proyek->_insert($data);
		echo 1;
	}
	
	function _update(){
		$proyek_id= $this->input->post('proyek_id');
		$data = array(
			'proyek_tglserahterima1' => ( date("Y-m-d",strtotime($this->input->post('proyek_tglserahterima1'))) == "1970-01-01")? null : date("Y-m-d",strtotime($this->input->post('proyek_tglserahterima1'))), 
			'proyek_tglserahterima2' => ( date("Y-m-d",strtotime($this->input->post('proyek_tglserahterima2')))== "1970-01-01")? null : date("Y-m-d",strtotime($this->input->post('proyek_tglserahterima2'))), 
			'proyek_kode' => $this->input->post('proyek_kode'), 
			'proyek_nama' => $this->input->post('proyek_nama'), 
			'm_cabang_id' => $this->input->post('m_cabang_id'), 
			'proyek_pm' => $this->input->post('proyek_pm'), 
			'proyek_jenis' => $this->input->post('proyek_jenis'), 
			'proyek_tahunmulai' => $this->input->post('proyek_tahunmulai'), 
			'proyek_nkawal' => $this->input->post('proyek_nkawal'), 
			'proyek_nkadd' => $this->input->post('proyek_nkadd'), 
			'proyek_nkakhir' => $this->input->post('proyek_nkakhir'), 
			'proyek_cp' => $this->input->post('proyek_cp'), 
			'proyek_kontruksijenis' => $this->input->post('proyek_kontruksijenis'), 
			'proyek_sumberdana' => $this->input->post('proyek_sumberdana'), 
			'proyek_jeniskontrak' => $this->input->post('proyek_jeniskontrak'), 
			'proyek_jaminanpelaksanaan' => $this->input->post('proyek_jaminanpelaksanaan'), 
			'proyek_jaminanpenawaran' => $this->input->post('proyek_jaminanpenawaran'), 
			'proyek_retensi' => $this->input->post('proyek_retensi'), 
			'proyek_denda' => $this->input->post('proyek_denda'), 
			'proyek_carapembayaran' => $this->input->post('proyek_carapembayaran'), 
			'proyek_uangmuka' => $this->input->post('proyek_uangmuka'), 
			'proyek_jangkawaktusetifikasi' => $this->input->post('proyek_jangkawaktusetifikasi'), 
			'proyek_jangkawaktupembayaran' => $this->input->post('proyek_jangkawaktupembayaran'), 
			
			'proyek_sys_owner'	=> $_SESSION[SESSION_USERID],
			'proyek_sys_update'=> date("Y-m-d h:i:s"));
			$this->m_proyek->_update($data,$proyek_id);
		echo 1;
 	}
	
	function _delete(){
		$proyek_id= $this->input->post('proyek_id');
		$data = array(
			'proyek_sys_active' => '0',
			'proyek_sys_owner'	=> $_SESSION[SESSION_USERID],
			'proyek_sys_delete'=> date("Y-m-d h:i:s")
		);
		$this->m_proyek->_delete($data,$proyek_id);
		echo 1;
	}
	
	function _detail(){
		$this->load->helper("date");
		$data['proyek_id'] = numeric_post("proyek_id");
		$data = $this->m_proyek->_detail($data);
		$data["data_m_proyek_jenis"] = array("","Reguler","JO");
		$data["data_proyek_sumberdana"] = array("","APBN", "APBD", "Swasta");
		$data["data_proyek_jeniskontrak"] = array("","Lump Sum", "Unit Price", "Gabungan", "Turn Key");
		$data["data_dokumen_asli"] = array("","Asli","Copy");
		$this->firephp->log($data);
		$html = $this->load->view("print/p_detaildokumen",$data,true);
		$file = fopen("print/detail.html","w+");
		fwrite($file,$html);
		fclose($file);
		echo 1;
	}
}